package cn.tedu.mall.product.service.impl;

import cn.tedu.mall.common.exception.CoolSharkServiceException;
import cn.tedu.mall.common.pojo.product.dto.PictureAddNewBatchDTO;
import cn.tedu.mall.common.pojo.product.dto.PictureAddNewDTO;
import cn.tedu.mall.common.pojo.product.dto.PictureUploadBatchDTO;
import cn.tedu.mall.common.pojo.product.dto.PictureUploadSingleDTO;
import cn.tedu.mall.common.pojo.product.model.Picture;
import cn.tedu.mall.common.pojo.product.vo.AlbumStandardVO;
import cn.tedu.mall.common.pojo.product.vo.PictureSimpleVO;
import cn.tedu.mall.common.pojo.product.vo.PictureStandardVO;
import cn.tedu.mall.common.restful.JsonPage;
import cn.tedu.mall.common.restful.JsonResult;
import cn.tedu.mall.common.restful.ResponseCode;
import cn.tedu.mall.product.mapper.AlbumMapper;
import cn.tedu.mall.product.mapper.PictureMapper;
import cn.tedu.mall.product.service.IPictureService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
@Service
public class PictureServiceImpl implements IPictureService {
    @Autowired
    PictureMapper pictureMapper;
    @Autowired
    AlbumMapper albumMapper;
    @Override
    public void addNew(PictureAddNewDTO pictureAddNewDTO) {
        String  url = pictureAddNewDTO.getUrl();
        PictureStandardVO pictureByUrl = pictureMapper.getByUrl(url);
        if (pictureByUrl != null) {
            throw   new CoolSharkServiceException(ResponseCode.CONFLICT,"新增图片失败,此URL对应的图片已经存在");
        }

        AlbumStandardVO album = albumMapper.getById(pictureAddNewDTO.getAlbumId());
        if (album == null) {
            throw   new CoolSharkServiceException(ResponseCode.CONFLICT,"相册不存在,可能已经被删除");
        }

        int pictureCount = pictureMapper.countByAlbumId(pictureAddNewDTO.getAlbumId());
        int cover = pictureCount == 0 ? 1 : 0;

        Picture picture = new Picture();
        BeanUtils.copyProperties(pictureAddNewDTO, picture);
        picture.setCover(cover);

        int insert = pictureMapper.insert(picture);
        if (insert!=1){
            throw   new CoolSharkServiceException(ResponseCode.INTERNAL_SERVER_ERROR,"新增图片失败服务器异常");
        }


    }

    @Override
    public void bindBatchToAlbum(PictureAddNewBatchDTO[] pictureDTO) {


    }

    @Override
    public PictureSimpleVO uploadSingle(PictureUploadSingleDTO pictureUploadSingleDTO) {
        return null;
    }

    @Override
    public PictureSimpleVO uploadSingleToAlbum(Long albumId, PictureUploadSingleDTO pictureUploadSingleDTO) {
        return null;
    }

    @Override
    public List<PictureSimpleVO> uploadBatch(PictureUploadBatchDTO pictureUploadBatchDTO) {
        return null;
    }

    @Override
    public void setCover(Long id) {

    }

    @Override
    public JsonPage<PictureStandardVO> listByAlbumId(Long albumId, Integer pageNum, Integer pageSize) {
        PageHelper.startPage(pageNum,pageSize);
        List<PictureStandardVO> pictureStandardVOS = pictureMapper.listByAlbumId(albumId);
        return JsonPage.restPage(new PageInfo<>(pictureStandardVOS));
    }

//    @Override
//    public JsonResult<List<PictureStandardVO>> queryByAlbumId(Long albumId) {
//        List<PictureStandardVO> pictureStandardVOS = pictureMapper.listByAlbumId(albumId);
//        return JsonResult.ok(pictureStandardVOS);
//    }
}
